
#include "keyscan.h"


void KeyScan(NormalButton *key)
{ 
  uint8_t ReadData = digitalRead(key->PIN) ^ 0x01;   // 1  读键值   
  key->Trg  = ReadData & (ReadData ^ key->Cont);              // 2  得到按下触发值  
  key->Release = ReadData ^ key->Trg ^ key->Cont;                  // 3  得到释放触发值          
  key->Cont = ReadData;                                  // 4  得到所有未释放的键值                                
  // Serial.printf("key.trg=%d\t",key->Trg);
  // Serial.printf("key.readdata=%d\t",ReadData);
  // Serial.printf("key.cont=%d\t",key->Cont);
  // Serial.printf("key.release=%d\t",key->Release);
  // Serial.printf("key.cnt_plus=%d\r\n",key->cnt_plus);
  // Serial.printf("key.cnt_flag=%d\r\n",key->cnt_flag);
  if (key->Trg)                    
  {                    
    //trg();                   //执行你想执行的任何代码  
    key_trg(key->PIN);
  }
  if (key->Cont)                    //长按
  {
    if(key->cnt_flag == 0)
      key->cnt_plus++;              //计时
    if(key->cnt_plus > 500)      // 计时时长
    {
      key->cnt_long_flag = 1;
      //cont();              // 你需要的执行的程序

      /*此函数在长按后只执行一次,若不想执行初始化为空函数即可*/
      {
        key_cont(key->PIN);
      }
      
      key->cnt_plus = 0;
      key->cnt_flag = 1;
    } 

    /*此函数在长按后重复执行，若不想执行初始化为空函数即可*/
    Serial.printf("cntlongflag=%d\r\n", key->cnt_long_flag);
    if(key->cnt_long_flag == 1)
    {
      Serial.println("kk");
      key_cont_long(key->PIN);
      delay(200);
    }  
  }
  if(key->Release)
  {
    key->cnt_flag = 0; 
    key->cnt_plus = 0;
    if(key->cnt_long_flag == 1)
      key->cnt_long_flag = 0;
    else
      key_release(key->PIN);
  }   
}

